using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CILDiffLib;
//using CILDiffLibPrime;

namespace CILDiff
{
    public partial class CILDiff : Form
    {
        public CILDiff()
        {
            InitializeComponent();
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            Visit visit = new Visit();
            visit.CompareFiles(txtVA.Text, txtVB.Text);

            Topics t = new Topics();
            t.Bind(visit.Topics);
            t.ShowDialog();
        }

        private void btnLoadA_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (DialogResult.OK == ofd.ShowDialog())
            {
                txtVA.Text = ofd.FileName;
            }
        }

        private void btnLoadB_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (DialogResult.OK == ofd.ShowDialog())
            {
                txtVB.Text = ofd.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string a = "a\nb\na\nb\na\nb\nc\na\na\n";
            //string b = "x\nb\na\ne\nb\na\ne\nb\ne\nc\na\n";

            //Diff.Item[] it = Diff.DiffText(a, b);
            //string diff = TestHelper(a,b,it);
            //MessageBox.Show(diff);
        }

        //public string TestHelper(string a, string b, Diff.Item[] f)
        //{
        //    StringBuilder ret = new StringBuilder();
        //    for (int n = 0; n < f.Length; n++)
        //    {
        //        //ret.Append(f[n].deletedA.ToString() + "." + f[n].insertedB.ToString() + "." + f[n].StartA.ToString() + "." + f[n].StartB.ToString() + "*");
        //        if (f[n].deletedA > 0 && f[n].insertedB == 0)
        //        {
        //            ret.Append("[deleted] ");
        //            ret.Append(a[f[n].StartA*2]);
        //        }
        //        else if (f[n].deletedA > 0 && f[n].insertedB > 0)
        //        {
        //            ret.Append("[changed] ");
        //            ret.Append(b[f[n].StartB * 2]);
        //        }
        //        else if (f[n].deletedA == 0 && f[n].insertedB > 0)
        //        {
        //            ret.Append("[added] ");
        //            ret.Append(b[f[n].StartB * 2]);
        //        }
        //    }
        //    // Debug.Write(5, "TestHelper", ret.ToString());
        //    return (ret.ToString());
        //}

        private void btnCompareRevs_Click(object sender, EventArgs e)
        {
            CompareRevisions compare = new CompareRevisions();
            CILDiffLib.EntityVocabularyActivity activity = compare.CompareRevs(txtBaseDir.Text, txtAssemblyNames.Text.Split(','));
            //int s = activity.EntityActivity.Count;

            // Entity Activity across revisions.
            System.IO.StreamWriter sw = new System.IO.StreamWriter("EntityActivity.txt");
            foreach (Hashtable rev in activity.EntityActivity)
            {
                sw.WriteLine("topic " + rev.Count);
                sw.WriteLine("---------------------");
                foreach (string key in rev.Keys)
                {
                    sw.WriteLine(rev[key] + ":" + key);
                }
            }
            sw.Close();

            // Method vocabulary across revisions.
            sw = new System.IO.StreamWriter("MethodVocabulary.txt");
            foreach (Hashtable rev in activity.MethodVocabulary)
            {
                sw.WriteLine("methods " + rev.Count);
                sw.WriteLine("---------------------");
                foreach (string method in rev.Keys)
                {
                    sw.WriteLine(method);
                    sw.WriteLine("{");
                    foreach( string type in (ArrayList)rev[method] )
                    {
                        sw.WriteLine(type);
                    }
                    sw.WriteLine("}");
                }
            }
            sw.Close();
        }

        private void btnLoadBaseDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                txtBaseDir.Text = fd.SelectedPath;
            }
        }
    }
}